package com.rumenz.controller;


import com.rumenz.controller.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;



@RestController
public class RumenzController {

    @Autowired
    UserService userService;
    //添加数据
    //http://127.0.0.1:8080/add?name=MyBatisPlus&domain=https://rumenz.com&age=10
    //暂时不支持中文
    @GetMapping("/add")
    public String index(@RequestParam("name") String name,@RequestParam("domain") String domain,@RequestParam("age") Integer age){
        Integer save = userService.save(name, domain, age);
        return "ok";
    }
//    查询所有数据
//    http://127.0.0.1:8080/list
    @GetMapping("/list")
    public List<User> list(){
        List<User> userlist = userService.list();
        return userlist;
    }

    //更新数据
    //http://127.0.0.1:8080/update?id=1&domain=https://rumenz.com&name=dongfangbubaiupdate&age=30
    @GetMapping("/update")
    public String update(@RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam("domain") String domain,@RequestParam("age") Integer age){
        Integer save = userService.update(id,name, domain, age);

        return "ok";
    }
    //删除数据
    //http://127.0.0.1:8080/delete?id=1
    @GetMapping("/delete")
    public String delete(@RequestParam("id") Integer id){
        Integer save = userService.delete(id);
        return "ok";
    }
    //通过Id查询数据,通过自定义SQL
    //http://127.0.0.1:8080/getById?id=1
    @GetMapping("/getById")
    public User getById(@RequestParam("id") Integer id){
//        User user = userService.getByById(id);
        User user=userService.getById(id);
        return user;
    }
}
